//
//  AboutUsViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/28.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "AboutUsViewController.h"
#import "MineServer.h"
#import "WebInfoViewController.h"
@interface AboutUsViewController ()
@property (nonatomic,strong) MineServer*mineServer;
@end

@implementation AboutUsViewController
- (instancetype)init{
    if (self == [super init]) {
        self = [super initWithStyle:UITableViewStylePlain];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getData];
}
- (void)getData{
    [self.mineServer getAboutUsComplete:^(BOOL success, NSString * _Nonnull msg) {
        if (success) {
            AboutUsModel*update = [AboutUsModel new];
            update.title = XLocalized(@"检测新版");
            [self.mineServer.aboutUsArr addObject:update];
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}

- (void)setupUI{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, AdaptedWidth(200))];
    UIImageView*imageV = [UIImageView new];
    [headView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(74), AdaptedWidth(70)));
    }];
    imageV.image = IMAGE_NAMED([self GetAppIconName]);
    UILabel *versionLab = [UILabel new];
    versionLab.textColor = UIColorHex(333333);
    versionLab.font = kDefaultFont(15);
    [headView addSubview:versionLab];
    [versionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(imageV.mas_bottom).offset(AdaptedWidth(17));
    }];
    versionLab.text = [NSString stringWithFormat:@"%@: v%@",XLocalized(@"版本号"),CurrentAppVersion];
    self.tableView.tableHeaderView = headView;
    self.tableView.rowHeight = AdaptedWidth(60);
    self.tableView.tableFooterView = [UIView new];
}
-(NSString*)GetAppIconName{
    NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    return icon;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineServer.aboutUsArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.mineServer.aboutUsArr[indexPath.row].title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == self.mineServer.aboutUsArr.count-1) {
        ///检查更新
        [MBProgressHUD showLoadTitile:@"" ToView:self.view];
        [self.mineServer getUpgradeVer:CurrentAppVersion ompletion:^(BOOL success, NSString * _Nonnull msg) {
            [MBProgressHUD hideHUDForView:self.view];
            if (success) {
                if (self.mineServer.updateM) {
                    [XAlertTool alertTitle:msg message:[self.mineServer.updateM.update_content componentsJoinedByString:@"\n"] cancelAction:self.mineServer.updateM.force_update?nil:^{
                        
                    } sureAction:^{
                        if (@available(iOS 10.0, *)) {
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.mineServer.updateM.download_url] options:@{} completionHandler:nil];
                        } else {
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.mineServer.updateM.download_url]];
                        }
                    }];
                }else{
                    [XAlertTool alertTitle:msg message:nil cancelAction:nil sureAction:^{
                        
                    }];
                }
            }else{
                [self.view makeToast:msg];
            }
        }];
        return;
    }
    WebInfoViewController *vc = [WebInfoViewController new];
    vc.gotoURL =  self.mineServer.aboutUsArr[indexPath.row].url;
    vc.title =  self.mineServer.aboutUsArr[indexPath.row].title;
    [self.navigationController pushViewController:vc animated:YES];
}

- (MineServer*)mineServer{
    if (!_mineServer) {
        _mineServer = [MineServer new];
    }
    return _mineServer;
}
@end
